package com.scheduler.controller;

import com.scheduler.pojo.AddMessage;
import com.scheduler.pojo.Result;
import com.scheduler.pojo.ResultMessage;
import com.scheduler.pojo.TypeNum;
import com.scheduler.service.ResultService;
import com.scheduler.spark.HopSearchWords;
import org.ansj.domain.Term;
import org.ansj.splitWord.analysis.ToAnalysis;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import scala.Tuple2;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Controller
public class loginController {
    @Autowired
    private ResultService resultService;

    @Autowired
    private HopSearchWords hopSearchWords;

    @RequestMapping("/index")
    public String getIndex(){
        return "Index";
    }

    @RequestMapping("/tologin")
    public String getLogin(){
        return "Login";
    }

    @RequestMapping("/vip1")
    public String getQuery(){
        return "Query";
    }

    @RequestMapping("/message")
    public String getNoLogin(){
        return "Message";
    }

    @RequestMapping("/vip2")
    public String getAdd(){
        return "Add";
    }

    @RequestMapping("/vip3")
    public String getShow(){
        return "Show";
    }

    @RequestMapping("/vip4")
    public String getUpdate(){
        return "Update";
    }

    @ResponseBody
    @PostMapping(value = "toData")
    public Map<String, Result> getResult(@RequestBody Map<String,Object> map){
        String mess = (String)map.get("mess");
        Integer start = (Integer)map.get("start");
        Integer end = (Integer)map.get("end");
        List<Term> list = ToAnalysis.parse(mess).getTerms();
        //将分词存到list集合中
        List<String> ls = new ArrayList<String>();
        for (int i =0;i<list.size();i++){
            //拿到分词后的词语
            String words = list.get(i).getName();
            ls.add(words);
        }
        List<String> ql = resultService.getType();
        List<String> rl = new ArrayList<String>();
        for (int j=0;j<ls.size();j++){
            if(ql.contains(ls.get(j))){
                rl.add(ls.get(j));
            }
        }
        List<Tuple2<String, Integer>> toplist = hopSearchWords.WordCount(rl);
        String str = "(";
        for(int q = 0;q<toplist.size();q++){
            str += "'"+toplist.get(q)._1+"',";
        }
        str+="'') limit "+start+","+end;
        System.out.println(str);
        List<Result> l = resultService.getResult(str);
        Map<String,Result> m = new HashMap<String,Result>();
        int num =1;
        for (Result a : l){
            m.put(String.valueOf(num),a);
            num++;
            System.out.println(a.getType());
        }
        Integer n = resultService.QueryCount(str);
        Result rt = new Result();
        rt.setResult(String.valueOf(n));
        m.put("pagecount",rt);
        return m;
    }

    @ResponseBody
    @PostMapping(value = "addData")
    public Map<String, AddMessage> addResult(@RequestBody Map<String,Object> map){
        String type = (String)map.get("type");
        String info = (String)map.get("info");
        String user = (String)map.get("user");
        String emp = (String)map.get("emp");
        Integer start = (Integer)map.get("start");
        Integer end = (Integer)map.get("end");
        String str ="";
        String str2 = "";
        str = "'"+type+"',"+"'"+info+"',"+"'"+user+"',"+"'"+emp+"',"+"null,"+"0";
        System.out.println(str);
        resultService.addResult(str);
        str2 = " limit "+start+","+end;
        Map<String,AddMessage> m = new HashMap<String,AddMessage>();
        List<AddMessage> l = resultService.getAdd(str2);
        int num =1;
        for(AddMessage b : l) {
            m.put(String.valueOf(num),b);
            num++;
        }
        Integer n = resultService.QueryCountTotal();
        AddMessage rt = new AddMessage();
        rt.setResult(String.valueOf(n));
        m.put("pagecount",rt);
        return m;
    }

    @ResponseBody
    @PostMapping(value = "addDataShow")
    public Map<String, AddMessage> addResultShow(@RequestBody Map<String,Object> map){
        Integer start = (Integer)map.get("start");
        Integer end = (Integer)map.get("end");
        String str2 = "";
        str2 = " limit "+start+","+end;
        System.out.println(str2);
        Map<String,AddMessage> m = new HashMap<String,AddMessage>();
        List<AddMessage> l = resultService.getAdd(str2);
        int num =1;
        for(AddMessage b : l) {
            m.put(String.valueOf(num),b);
            num++;
        }
        Integer n = resultService.QueryCountTotal();
        AddMessage rt = new AddMessage();
        rt.setResult(String.valueOf(n));
        m.put("pagecount",rt);
        return m;
    }

    @ResponseBody
    @PostMapping(value = "addResultDataShow")
    public Map<String, ResultMessage> addResultDataShow(@RequestBody Map<String,Object> map){
        Integer start = (Integer)map.get("start");
        Integer end = (Integer)map.get("end");
        String str2 = "";
        str2 = " limit "+start+","+end;
        Map<String,ResultMessage> m = new HashMap<String,ResultMessage>();
        List<ResultMessage> l = resultService.getRmess(str2);
        int num =1;
        for(ResultMessage b : l) {
            m.put(String.valueOf(num),b);
            num++;
        }
        Integer n = resultService.QueryCountTotals();
        ResultMessage rt = new ResultMessage();
        rt.setResult(String.valueOf(n));
        m.put("pagecount",rt);
        return m;
    }

    @ResponseBody
    @PostMapping(value = "updateResultData")
    public Map<String, ResultMessage> updateResultData(@RequestBody Map<String,Object> map){
        Integer start = (Integer)map.get("start");
        Integer end = (Integer)map.get("end");
        String str2 = "";
        str2 = " limit "+start+","+end;
        String id = (String)map.get("id");
        String result = (String)map.get("result");
        System.out.println(id+result+"qqqqqqqqqqqqqqqqqqqqqqqqqq");
        ResultMessage rm = new ResultMessage();
        rm.setId(Integer.parseInt(id));
        rm.setType(null);
        rm.setMessage(null);
        rm.setUser(null);
        rm.setEmp(null);
        rm.setResult(result);
        rm.setFlag(null);
        resultService.addRmess(rm);
        Map<String,ResultMessage> m = new HashMap<String,ResultMessage>();
        List<ResultMessage> l = resultService.getRmess(str2);
        int num =1;
        for(ResultMessage b : l) {
            m.put(String.valueOf(num),b);
            num++;
        }
        Integer n = resultService.QueryCountTotals();
        ResultMessage rt = new ResultMessage();
        rt.setResult(String.valueOf(n));
        m.put("pagecount",rt);
        return m;
    }

    @ResponseBody
    @PostMapping(value = "updateResultDataflag")
    public Map<String, ResultMessage> updateResultDataflag(@RequestBody Map<String,Object> map){
        Integer start = (Integer)map.get("start");
        Integer end = (Integer)map.get("end");
        String str2 = "";
        str2 = " limit "+start+","+end;
        String id = (String)map.get("id");
        String flag = (String)map.get("flag");
        ResultMessage rm = new ResultMessage();
        rm.setId(Integer.parseInt(id));
        rm.setType(null);
        rm.setMessage(null);
        rm.setUser(null);
        rm.setEmp(null);
        rm.setResult(null);
        rm.setFlag(Integer.parseInt(flag));
        resultService.addRmessflag(rm);
        Map<String,ResultMessage> m = new HashMap<String,ResultMessage>();
        List<ResultMessage> l = resultService.getRmess(str2);
        int num =1;
        for(ResultMessage b : l) {
            m.put(String.valueOf(num),b);
            num++;
        }
        Integer n = resultService.QueryCountTotals();
        ResultMessage rt = new ResultMessage();
        rt.setResult(String.valueOf(n));
        m.put("pagecount",rt);
        return m;
    }

    @ResponseBody
    @PostMapping(value = "toquerylimitdata")
    public Map<String, Result> totalPageCount(@RequestBody Map<String,Object> map){
        String mess = (String)map.get("mess");
        Integer start = (Integer)map.get("start");
        Integer end = (Integer)map.get("end");
        List<Term> list = ToAnalysis.parse(mess).getTerms();
        //将分词存到list集合中
        List<String> ls = new ArrayList<String>();
        for (int i =0;i<list.size();i++){
            //拿到分词后的词语
            String words = list.get(i).getName();
            ls.add(words);
        }
        List<String> ql = resultService.getType();
        List<String> rl = new ArrayList<String>();
        for (int j=0;j<ls.size();j++){
            if(ql.contains(ls.get(j))){
                rl.add(ls.get(j));
            }
        }
        List<Tuple2<String, Integer>> toplist = hopSearchWords.WordCount(rl);
        String str = "(";
        for(int q = 0;q<toplist.size();q++){
            str += "'"+toplist.get(q)._1+"',";
        }
        str+="'') limit "+start+","+end;
        System.out.println(str);
        List<Result> l = resultService.getResult(str);
        Map<String,Result> m = new HashMap<String,Result>();
        int num =1;
        for (Result a : l){
            m.put(String.valueOf(num),a);
            num++;
            System.out.println(a.getType());
        }
        return m;
    }

    @ResponseBody
    @PostMapping(value = "toDistinctTypeNum")
    public Map<String,TypeNum> toDistinctTypeNum(@RequestBody Map<String,Object> map) {
        List<TypeNum> l = resultService.DistinctTypeNum();
        Integer n = resultService.QueryCountTotals();
        Map<String,TypeNum> m = new HashMap<String,TypeNum>();
        int num =1;
        for(TypeNum p : l){
            m.put(String.valueOf(num),p);
            System.out.println(p.getNumcount());
            num++;
        }
        TypeNum tn = new TypeNum();
        tn.setNumcount(n);
        m.put("countnum",tn);
        return m;
    }
}
